
package com.ami.iusb.protocol;

import com.ami.iusb.EncryptionException;
import com.ami.iusb.RedirectionException;
import com.ami.kvm.jviewer.Debug;
import com.ami.kvm.jviewer.JViewer;
import com.ami.kvm.jviewer.communication.Connection;
import com.ami.kvm.jviewer.gui.JViewerApp;
import com.ami.kvm.jviewer.gui.LocaleStrings;
import com.ami.vmedia.gui.VMDialog;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintStream;
import java.net.InetAddress;
import java.net.Socket;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.channels.Selector;
import javax.net.ssl.SSLSocket;

public class PacketMaster {
	private Socket packetSock;
	private Socket Sock;
	private SSLSocket packetSockssl;
	private Selector packetSel;
	private RedirProtocol protocol;
	private ByteBuffer netIn;
	private ByteBuffer netOut;
	private ByteBuffer appIn;
	private ByteBuffer appOut;
	private String host;
	private int port;
	private boolean wakeup = false;

	private static final int IUSB_HDR_SIZE = 32;

	public PacketMaster(String paramString, int paramInt, boolean paramBoolean1, RedirProtocol paramRedirProtocol,
			boolean paramBoolean2) {
		this.host = paramString;
		this.port = paramInt;

		this.protocol = paramRedirProtocol;
	}

	public void setupBuffers(int paramInt1, int paramInt2) {
		this.netIn = ByteBuffer.allocateDirect(paramInt1);
		this.netOut = ByteBuffer.allocateDirect(paramInt2);

		this.appIn = this.netIn;
		this.appOut = this.netOut;
		Debug.out.println("No SSL, allocating only netIn and netOut...");
		Debug.out.println("Allocated netOut buffer of " + this.netOut.capacity() + " bytes");
		Debug.out.println("Allocated netIn buffer of " + this.netIn.capacity() + " bytes");
	}

	public void setupBuffers(ByteBuffer paramByteBuffer1, ByteBuffer paramByteBuffer2) {
		this.netIn = paramByteBuffer1;
		this.netOut = paramByteBuffer2;

		this.appIn = this.netIn;
		this.appOut = this.netOut;

		this.netIn.clear();
		this.netOut.clear();
		Debug.out.println("No SSL, using provided buffers exclusively");
	}

	public void setBufferEndianness(ByteOrder paramByteOrder1, ByteOrder paramByteOrder2) {
		this.appIn.order(paramByteOrder1);
		this.appOut.order(paramByteOrder2);
	}

	public int connectVmedia(boolean paramBoolean) {
		try {
			int i = paramBoolean == true ? 2 : 0;

			this.packetSock = JViewerApp.getInstance().getConnection().createSocket(InetAddress.getByName(this.host),
					this.port, i);
		} catch (IOException localIOException) {
			Debug.out.println(localIOException);
			return -1;
		}

		if (this.packetSock == null) {
			if ((paramBoolean) && (JViewerApp.getInstance().getConnection().getConnErrCode() == Connection.SUCCESS)) {
				JViewerApp.getInstance().getVMDialog().disposeVMDialog();
			} else {
				return -1;
			}
		}
		setSock(this.packetSock);
		return 0;
	}

	public Socket getSock() {
		return this.Sock;
	}

	public void setSock(Socket paramSocket) {
		this.Sock = paramSocket;
	}

	public void VmediaSockclose() throws IOException {
		if (JViewer.isSinglePortEnabled()) {
			if (getSock() != null) {
				getSock().close();
			}
		} else {
			if (this.packetSock != null) {
				this.packetSock.close();
			}
			if (this.packetSockssl != null) {
				this.packetSockssl.close();
			}
		}
	}

	public void wakeup() {
		this.wakeup = true;

		if (this.packetSel.isOpen()) {
			this.packetSel.wakeup();
		}
	}

	public void sendPacket(RedirPacket paramRedirPacket) throws IOException, EncryptionException {
		this.appOut.clear();
		this.netOut.clear();

		paramRedirPacket.writePacket(this.appOut);
		this.appOut.flip();

		byte[] arrayOfByte = new byte[this.netOut.remaining()];
		this.netOut.get(arrayOfByte);
		getSock().getOutputStream().write(arrayOfByte);
	}

	public RedirPacket receivePacket(boolean paramBoolean) throws IOException, RedirectionException {
		int i = 0;
		RedirPacket localRedirPacket = null;

		if (this.appIn.position() != 0) {
			this.appIn.flip();
			localRedirPacket = this.protocol.getPacket(this.appIn);
			if (localRedirPacket == null) {

				this.appIn.position(this.appIn.limit());
				this.appIn.limit(this.appIn.capacity());
			} else {
				Debug.out.println("Got a packet!");
			}
		}

		while (localRedirPacket == null) {
			byte[] arrayOfByte1 = new byte[32];

			i = read_data(arrayOfByte1);

			if (i < 0) {
				throw new IOException(LocaleStrings.getString("8_2_PACKETMAST"));
			}

			this.netIn.put(arrayOfByte1);
			this.netIn.position(12);

			long l = this.netIn.getInt() & 0xFFFFFFFF;
			byte[] arrayOfByte2 = new byte[(int) l];

			i = 0;
			i = read_data(arrayOfByte2);

			if (i < 0) {
				throw new IOException(LocaleStrings.getString("8_2_PACKETMAST"));
			}
			this.netIn.position(32);

			this.netIn.put(arrayOfByte2, 0, arrayOfByte2.length);
			this.netIn.position(0);
			this.netIn.limit(arrayOfByte1.length + arrayOfByte2.length);

			localRedirPacket = this.protocol.getPacket(this.netIn);
			if (localRedirPacket == null) {
				Debug.out.println("Can't build a packet from input data");
				this.appIn.position(this.appIn.limit());
				this.appIn.limit(this.appIn.capacity());
			} else {
				Debug.out.println("Got a packet!");
			}
		}
		if (paramBoolean) {
			this.appIn.compact();
		}
		return localRedirPacket;
	}

	public RedirPacket receivePacket() throws IOException, RedirectionException {
		return receivePacket(true);
	}

	public void clearBuffer() {
		this.appIn.compact();
	}

	public int read_data(byte[] paramArrayOfByte) {
		int i = 0;
		int j = paramArrayOfByte.length;
		int k = 0;

		while (j != 0) {
			try {
				k = getSock().getInputStream().read(paramArrayOfByte, i, j);
			} catch (IOException localIOException) {
			}

			if (k <= 0) {
				System.out.println("SOCKET FAILURE");
				return k;
			}
			j -= k;
			i += k;
		}
		k = paramArrayOfByte.length;
		return k;
	}
}

/*
 * Location:
 * D:\Users\tianlin\Downloads\JViewer.jar!\com\ami\iusb\protocol\PacketMaster.
 * class Java compiler version: 5 (49.0) JD-Core Version: 0.7.1
 */